9 Lecture
CS506
Midterm & Final Term Short Notes
Abstract Classes and Interfaces
Abstract classes and interfaces are fundamental in OOP. Abstract classes provide a blueprint with partially implemented methods, while interfaces define method contracts. Classes extend abstract classes and implement interfaces, ensuring code st
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
Certainly, here are 10 multiple-choice questions (MCQs) related to Abstract Classes and Interfaces, along with their solutions and multiple options:
**Question 1: What is an abstract class in Java?**
a) A class that cannot be instantiated
b) A class that can only have static methods
c) A class with no methods
d) A class without any instance variables
**Solution: a) A class that cannot be instantiated**
**Question 2: What is the main purpose of an abstract class?**
a) To provide multiple inheritance in Java
b) To define a base template for other classes
c) To hide the implementation details of a class
d) To restrict access to methods and variables
**Solution: b) To define a base template for other classes**
**Question 3: What is the keyword used to define an abstract class in Java?**
a) abstract
b) class
c) interface
d) extends
**Solution: a) abstract**
**Question 4: Can an abstract class have concrete (fully implemented) methods?**
a) Yes, only one concrete method
b) No, all methods must be abstract
c) Yes, any number of concrete methods
d) Yes, but only in subclasses
**Solution: c) Yes, any number of concrete methods**
**Question 5: What is an interface in Java?**
a) A concrete class
b) A blueprint for an object
c) A type of array
d) A collection of methods without implementations
**Solution: d) A collection of methods without implementations**
**Question 6: Can a class implement multiple interfaces in Java?**
a) Yes, but only if they have the same method names
b) No, a class can implement only one interface
c) Yes, there's no limit to how many interfaces a class can implement
d) Yes, if the interfaces are in the same package
**Solution: c) Yes, there's no limit to how many interfaces a class can implement**
**Question 7: What is the keyword used to declare that a class is implementing an interface in Java?**
a) extends
b) implements
c) includes
d) uses
**Solution: b) implements**
**Question 8: Which of the following is true about abstract methods in interfaces?**
a) They are not allowed in interfaces
b) They must have a method body
c) They are implicitly public and abstract
d) They can be marked as final
**Solution: c) They are implicitly public and abstract**
**Question 9: Can an interface extend another interface in Java?**
a) No, interfaces cannot extend other interfaces
b) Yes, but only one interface can extend another
c) Yes, interfaces can extend multiple interfaces
d) Yes, but only if they are in the same package
**Solution: c) Yes, interfaces can extend multiple interfaces**
**Question 10: Which one allows for more flexibility in class design: abstract classes or interfaces?**
a) Abstract classes
b) Interfaces
c) Both provide equal flexibility
d) None, they provide the same level of flexibility
**Solution: b) Interfaces**
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
Certainly, here are 10 subjective short questions along with their answers related to Abstract Classes and Interfaces:
**Question 1: What is an abstract class, and why would you use it in Java?**
**Answer:** An abstract class is a class that cannot be instantiated and can have both abstract (unimplemented) and concrete (implemented) methods. It serves as a blueprint for other classes to inherit from, enabling code reuse and enforcing a common structure for subclasses.
**Question 2: Explain the concept of multiple inheritance and how Java deals with it using interfaces.**
**Answer:** Multiple inheritance refers to a class inheriting from more than one class. Java avoids the complications of multiple inheritance by using interfaces. A class can implement multiple interfaces, enabling it to inherit method signatures from multiple sources without inheriting implementation details.
**Question 3: How does an abstract class differ from an interface in Java?**
**Answer:** An abstract class can have both abstract and concrete methods, while an interface can only have abstract methods. A class can implement multiple interfaces, but it can only extend a single abstract class. Abstract classes can have instance variables, while interfaces can only have constants.
**Question 4: Can an abstract class have a constructor? Explain.**
**Answer:** Yes, an abstract class can have a constructor. The constructor is called when an instance of a concrete subclass is created. It can initialize instance variables or perform other necessary setup operations.
**Question 5: What are default methods in Java interfaces?**
**Answer:** Default methods are methods in interfaces that have a default implementation. They allow adding new methods to existing interfaces without breaking the implementing classes. Concrete classes implementing the interface can choose to override or use the default implementation.
**Question 6: How do you achieve method overloading and overriding in interfaces?**
**Answer:** Interfaces support method overloading by allowing multiple methods with the same name but different parameter lists. Method overriding in interfaces is similar to class inheritance; a subclass implementing an interface must provide a method with the same name, parameters, and return type as the parent interface.
**Question 7: Explain the importance of the "implements" keyword in Java interfaces.**
**Answer:** The "implements" keyword is used to signify that a class is adopting the contract defined by an interface. It ensures that the class provides implementations for all the methods declared in the interface, establishing a strong relationship between the class and the interface.
**Question 8: Can an interface extend a class in Java? Why or why not?**
**Answer:** No, an interface cannot extend a class in Java. An interface only defines method signatures, and it's not meant to inherit or extend class implementations. However, an interface can extend another interface to inherit its method signatures.
**Question 9: What is the purpose of a marker interface?**
**Answer:** A marker interface is an interface that does not declare any methods but is used to provide metadata about a class implementing it. Examples include the `Serializable` interface in Java, which indicates that a class can be serialized.
**Question 10: In which situations would you prefer using an abstract class over an interface, and vice versa?**
**Answer:** Use an abstract class when you want to provide a base implementation and share code among related subclasses. Use an interface when you want to define a contract that multiple unrelated classes can implement, enabling them to fulfill a common role without inheritance constraints.